package cn.changein.framework.db;

import org.apache.ibatis.mapping.DatabaseIdProvider;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

/**
 * <p>
 *     数据库id选择器
 * </p>
 *

 * @Date: 2020-10-27 15:23
 * @Copyright: companyName
 * @Version 1.0
 * @Since JDK1.8
 */
public class CustomDatabaseIdProvider implements DatabaseIdProvider {
    @Override
    public void setProperties(Properties p) {
    }

    @Override
    public String getDatabaseId(DataSource dataSource) throws SQLException {
        String url = dataSource.getConnection().getMetaData().getURL();

        if (url.contains("oracle")) {
            return DBID.ORACLE;
        } else if (url.contains("postgresql")) {
            return DBID.PG_SQL;
        } else if (url.contains("sqlserver")) {
            return DBID.MS_SQL;
        } else {
            return DBID.MYSQL;
        }
    }
}
